package com.tianqu.tms.www.finance.action;


import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.MobileMessageBo;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.entity.TmsSmsInfo;
import com.tianqu.tms.www.finance.actionform.PropertyDepositForm;
import com.tianqu.tms.www.finance.controller.PropertyDepositInputResultController;
import com.tianqu.tms.www.finance.viewhelper.PropertyDepositViewHelper;

public class PropertyDepositInputResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(PropertyDepositInputResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		PropertyDepositForm inputForm = (PropertyDepositForm)form;
		PropertyDepositViewHelper view = (PropertyDepositViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
		logger.info("property deposit,info:memberId:"+view.getMemberId()+" price:"+view.getPrice()+" type:"+view.getType()+" remark:"+view.getRemark());
		logger.info("测试短信信息 以及内容");
		TmsUserProfile profile = uc.getUserProfile();
		
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
		String insertTime = dateFormat.format(now);
		
		if(StringUtil.isNullOrBlank(inputForm.getMemberId()))
		{
			String[] param = {"商户信息"};
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}else if(StringUtil.isNullOrBlank(inputForm.getPrice())){
			String[] param = {"金额"};
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		int price  = 0;
		int margin = 0;
		String fees1 = null;
		String fees2 = null;
		//发送的短信内容
		//[物业保证金扣罚通知]$时间(时间为系统时间)+$短信内容（短信内容为文本框输入内容） [车集网]
		try {
			price = Integer.parseInt(StringUtil.removeDH(inputForm.getPrice()));
			margin = Integer.parseInt(StringUtil.removeDH(inputForm.getMargin()));
		} catch (Exception e) {
			String[] param = {"金额"};
			setErrorsToRequest(request, TmsConstants.U0014, param);
			return mapping.getInputForward();
		}		
		if("2".equals(inputForm.getType()) && price > margin){
			setErrorsToRequest(request, TmsConstants.U1033,null);
			return mapping.getInputForward();
		}
		if("1".equals(view.getType())){
	
			//动态拼缴费与扣费
			fees1="[物业保证金缴费通知]";
			fees2="缴费金额为:";
		}else if("2".equals(view.getType())){

			fees1="[物业保证金扣罚通知]";
			fees2="扣罚金额为";
		}
		String content=fees1+insertTime+fees2+price+inputForm.getRemark()+"[车集网] ";
		//在这里调用短信通知借口   如果DXTZTYype的值为2   则需要短信通知   否则不需要短信通知   
		if("2".equalsIgnoreCase(inputForm.getDxtzType())){		
			if(!(view.getPhone() == null&& view.getPhone().length() <= 0)){
				// 准备发短信
				TmsSmsInfo sms1 = new TmsSmsInfo();
				sms1.setSerial("00000");
				sms1.setSmsContent(content);
			    sms1.setInsertUserId(Integer.parseInt(view.getMemberId().trim()));
				sms1.setOwnerPhone(view.getPhone());
				 //将当前时间放到短信记录中
				sms1.setInsertTime(insertTime);
				sms1.insertSmsInfo(sms1);
				//
				MobileMessageBo msg = new MobileMessageBo();
				msg.setPhone(view.getPhone());
				msg.setContent(content);
				PullingExecutor.getInstance().submit(msg);
			}
			//
			if(!(view.getContactPhone()== null && view.getContactPhone().length() <= 0)){
				// 准备发短信
				TmsSmsInfo sms2 = new TmsSmsInfo();
				//添加流水号
				sms2.setSerial("00000");
				sms2.setSmsContent(content);
			    sms2.setInsertUserId(Integer.parseInt(view.getMemberId().trim()));
				sms2.setOwnerPhone(view.getContactPhone());
			      //将当前时间放到短信记录中
				sms2.setInsertTime(insertTime);
				sms2.insertSmsInfo(sms2);
				//发送消息给客户
				//
				MobileMessageBo msg = new MobileMessageBo();
				msg.setPhone(view.getContactPhone());
				msg.setContent(content);
				PullingExecutor.getInstance().submit(msg);
		}		
		}
		PropertyDepositViewHelper view1 = new PropertyDepositViewHelper ();
		view1.setPrice(StringUtil.removeDH(inputForm.getPrice()));
		view1.setType(inputForm.getType());
		view1.setMemberId(inputForm.getMemberId());
		view1.setRemark(inputForm.getRemark());
	    view1.setDxtzText(inputForm.getDxtzText());
        view1.setDxtzType(inputForm.getDxtzType());
        view1.setContactPhone(view.getContactPhone());
		uc.setAttribute(TmsConstants.VIEWHELP, view1);
		//
		PropertyDepositInputResultController controller = new PropertyDepositInputResultController();
		controller.execute(uc);
		
		//
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
